package cn.com.cis.web.actions;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import cn.com.cis.utils.FileUtils;

@Controller
@RequestMapping(value = "/drivers")
public class DriverJarsController {

    private void setNavigateBar(ModelAndView mv){
    	mv.addObject("icon", "icon-list");
        mv.addObject("title", "驱动管理");
    }

    @RequiresPermissions("配置管理:驱动管理:查看驱动列表")
    @RequestMapping(value = "/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1", required = false) int page, HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("drivers/list");
        setNavigateBar(mav);
        
        List<String> jarFiles = FileUtils.getDriverJars(request);
        
        int pageSize = 10;
        int totalPage = (jarFiles.size() - 1)/pageSize + 1;
        
        mav.addObject("jarFiles", jarFiles.subList(pageSize * (page - 1), pageSize * page > jarFiles.size() ? jarFiles.size() : pageSize * page));
        mav.addObject("page", page);
        mav.addObject("totalPage", totalPage);
        
        return mav;
    }

    @RequiresPermissions("配置管理:驱动管理:添加驱动")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView save(HttpServletRequest request, RedirectAttributesModelMap modelMap) {
		ModelAndView mav = new ModelAndView("redirect:/drivers/list");
		setNavigateBar(mav);

//		// 使用Apache文件上传组件处理文件上传
//		DiskFileItemFactory factory = new DiskFileItemFactory();
//		ServletFileUpload upload = new ServletFileUpload(factory);
//
//		// 解决上传文件名的中文乱码
//		upload.setHeaderEncoding("UTF-8");
//
//		List<FileItem> list = upload.parseRequest(request);
//		for (FileItem item : list) {
//			if (!item.isFormField()) { // 如果fileitem中封装的是上传文件
//				//得到上传的文件名称并检查合法性
//				String filename = item.getName();
//				if (filename == null || filename.trim().equals("") || !filename.endsWith(".jar")) {
//					modelMap.addFlashAttribute("error", "只能上传后缀为jar的文件！");
//					return mav;
//				}
//				
//				InputStream in = item.getInputStream();
//				FileOutputStream out = new FileOutputStream(FileUtils.getDriverFolder(request) + filename);
//				byte buffer[] = new byte[1024];
//				int len = 0;
//				while ((len = in.read(buffer)) > 0) {
//					out.write(buffer, 0, len);
//				}
//				in.close();
//				out.close();
//				
//				//删除处理文件上传时生成的临时文件
//				item.delete();
//			}
//		}

		return mav;
	}

    @RequiresPermissions("配置管理:驱动管理:删除驱动")
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public ModelAndView del(String fileName, HttpServletRequest request, RedirectAttributesModelMap modelMap) {
        ModelAndView mav = new ModelAndView("redirect:/drivers/list");
        setNavigateBar(mav);
        
        String webAppRoot = request.getSession().getServletContext().getRealPath("/");
        File jarFile = new File(webAppRoot + "/drivers/" + fileName);
        
        jarFile.delete();
        
        return mav;
    }

}
